/**
 * 角色分配
 */
Ext.define('BenBen.modules.system.role.RoleDistributeWindow', {
    extend : 'Ext.window.Window',
    requires: [
        'Ext.ux.CheckColumn',
       	'BenBen.modules.system.role.RoleGrid'
    ],
    width : 500,
    height : 500,
    title : '角色分配',
    border : false,
	initComponent : function() {
		var me = this;

		var roleGrid = Ext.create('BenBen.modules.system.role.RoleGrid',{
			isLoad : false,
			selModel : {
			    selType : 'checkboxmodel',
			    mode : 'SIMPLE'
			},
			tbarDefaultItems : '',
			tbarItems : null
		});
		
		roleGrid.store.on('load', function (store, records) {
			for(var i=0; i<records.length; i++) {
	            var record = records[i];
	            if(record.get('checked') == 1) {
	            	roleGrid.getSelectionModel().select(record,true);
	            }
	        }
	    });
		
		roleGrid.store.load({
			params : {
				username : me.userRecord.get('userId')
			}
		});
		
		var bbar = new Ext.Toolbar({
            items : [{
                xtype: 'tbfill'
            },{
                text : '确定',
                iconCls : 'formsave',
                handler : function() {	
                	var records = roleGrid.getView().getSelectionModel().getSelection(),
                     ids = [];
                	Ext.Array.each(records, function(rec){
                        ids.push(rec.get('id'));
                    });
                	Ext.Ajax.request({
                        url: 'system/user/role',
                        method: 'POST',
                        params : {
                        	userId : me.userRecord.get('userId'),
                        	roleIds : ids.join(',')
                        },
                        callback: function (options, success, response) {
                            if (success) {
                                if (Ext.decode(response.responseText).success) {
                                	Ext.Msg.confirm('成功','操作成功');
        						} else {
        							Ext.Msg.confirm('失败',Ext.decode(response.responseText).info);
        						}
                                me.hide();
                            } else {
                                Ext.Msg.confirm('失败','操作失败');
                            }
                        }
                    });
                }
            }, {
                text : '取消',
                iconCls : 'formcancle',
                handler : function() {
                	me.hide();
                }
            },{
                xtype: 'tbfill'
            }]
        });


		Ext.apply(this, {
			maximizable : true,
			closeAction : 'hide',
            border : false,
            bbar : bbar,
            layout : 'form',
			items : [roleGrid]
		});
		this.callParent(arguments);
	}

});